package com.hzit.servlet;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.template.engine.freemarker.SimpleStringTemplateLoader;
import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.IPage;
import com.github.pagehelper.Page;
import com.hzit.common.utils.PageResult;
import com.hzit.common.utils.R;
import com.hzit.common.utils.ResponseUtil;
import com.hzit.entity.Student;
import com.hzit.service.StudentService;
import com.hzit.service.impl.StudentServiceImpl;
import com.hzit.vo.StudentVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@WebServlet("/student")
public class StudentServlet extends HttpServlet {
	private StudentService studentService;

	// 1. 每次servlet加载时会主动调用
	@Override
	public void init() throws ServletException {
		studentService = new StudentServiceImpl();
	}

	//2. 每次请求时自动调用
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json;charset=utf-8");
		String cmd = req.getParameter("cmd");
		if(StrUtil.isNotBlank(cmd)){
			if("list".equals(cmd)){
				list(req,resp);
			}
			if("findByPage".equals(cmd)){
				findByPage(req,resp);
			}
			if("search".equals(cmd)){
				search(req,resp);
			}
		}
	}


	// 1. 列表学生
	private void list(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
		// 1.1 得到所有学生信息
		List<Student> students = studentService.findAll();
		// 1.2 转换为R对象
		R r = R.ok().data("list",students);
		// 1.3 转换为字符串并输出
		// resp.getWriter().println(JSON.toJSONString(r));
		// resp.getWriter().flush();
		ResponseUtil.write(resp,r);
	}
	// 2. 分页查询
	private void findByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 2.1 得到分页的参数
		String pageStr = req.getParameter("page");
		String pageSizeStr = req.getParameter("pageSize");
		// 2.2 转换为整数类型
		Integer page = new Integer(pageStr);
		Integer pageSize = new Integer(pageSizeStr);
		// 2.3 开始分页查询
		PageResult<Student> pageResult = studentService.findByPage(page,pageSize);
		// 2.4 放上面的对象到R对象中
		R r = R.ok().data("pageResult", pageResult);
		// 2.5 将上面的r对象转换为字符串并输出
		// resp.getWriter().println(JSON.toJSONString(r));
		// resp.getWriter().flush();
		ResponseUtil.write(resp,r);
	}
	// 3. 条件查询带分页
	private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 3.1 得到分页的参数
		String pageStr = req.getParameter("page");
		String pageSizeStr = req.getParameter("pageSize");
		// 3.2 转换为整数类型
		Integer page = new Integer(pageStr);
		Integer pageSize = new Integer(pageSizeStr);
		// 3.3 获取前端传入的对象并转换为对象（将传入的输入流转换为VO对象）
		ObjectMapper mapper = new ObjectMapper();
		StudentVO studentVO = mapper.readValue(req.getInputStream(), StudentVO.class);
		// 3.4 调用查询方法查询学生列表
		PageResult<Student> pageResult = studentService.search(page,pageSize,studentVO);
		// 3.5 放上面的对象到R对象中
		R r = R.ok().data("pageResult", pageResult);
		// 3.6 将上面的r对象转换为字符串并输出
		// resp.getWriter().println(JSON.toJSONString(r));
		// resp.getWriter().flush();
		ResponseUtil.write(resp,r);
	}
}
